<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>react-demo-22</title>
    <link rel="preconnect" href="https://fonts.googleapis.com" />
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
    <link href="../../../js/font.css" rel="stylesheet" />
  </head>
  <style lang="scss">
    * {
      font-family: "Noto Sans SC", sans-serif;
    }

    body {
      display: flex;
      justify-content: flex-end;
    }

    .title {
      display: inline-flex;
      border: 1px solid blue;
      border-radius: 3px;
    }
  </style>
  <body>
    <!-- 准备容器 -->
    <div id="main"></div>

    <!-- 引入react核心库 -->
    <script
      type="text/javascript"
      src="../../../js/react.development.min.js"
    ></script>
    <!-- 引入react-dom，用于操作dom -->
    <script
      type="text/javascript"
      src="../../../js/react-dom.development.min.js"
    ></script>

    <!-- 引入prop-types -->
    <script src="../../../js/prop-types.js"></script>

    <!-- 引入babel，用于编译jsx -->
    <script src="../../../js/babel.min.js"></script>

    <!-- 此处一定要写babel，用于提示babel对该内容进行编译 -->
    <script type="text/babel">
      class LifeCycleDemo extends React.Component {
        state = {
          opacity: 1,
        };

        handleClick = () => {
          // UnMount component
          ReactDOM.unmountComponentAtNode(document.getElementById("main"));
        };

        componentDidMount() {
          this.interval = setInterval(() => {
            let { opacity } = this.state;
            if (opacity - 0.1 <= 0) opacity = 1.1;
            this.setState({
              opacity: opacity - 0.1,
            });
          }, 200);
        }

        componentWillUnmount() {
          clearInterval(this.interval);
        }

        // render 里绝不能写 setState
        render() {
          return (
            <div>
              <h2 style={{ opacity: this.state.opacity }}>Text to react</h2>
              <button onClick={this.handleClick}>reaction</button>
            </div>
          );
        }
      }
      ReactDOM.render(<LifeCycleDemo />, document.getElementById("main"));
    </script>
  </body>
</html>
